package Hibernate.cn.edu.scau.cmi.lishuoxin.client;
import java.util.Set;

import Hibernate.cn.edu.scau.cmi.lishuoxin.dao.StudentDAO;
import Hibernate.cn.edu.scau.cmi.lishuoxin.dao.TeacherDAO;
import Hibernate.cn.edu.scau.cmi.lishuoxin.domain.Student;
import Hibernate.cn.edu.scau.cmi.lishuoxin.domain.Teacher;


public class TeacherPane extends BasePane {

	private Teacher teacherForAdd;
	private Teacher teacherForUpdate;
	private Teacher teacherForDelete;
	private Teacher teacherForShow;
	private Student studentForSelected;
	private Teacher teacherForSelected;
	private Set studentSet;
	
	private StudentDAO studentDAO = new StudentDAO();
	private TeacherDAO teacherDAO = new TeacherDAO();
	private AlertBox alert = new AlertBox();
	
	public TeacherPane() {
		super();
		init();
		setActions();
	}
	
	private void setActions() {
		addButton.setOnAction(e->addTeacher());
		updateButton.setOnAction(e->updateTeacher());
		deleteButton.setOnAction(e->deleteTeacher());
		showButton.setOnAction(e->showTeacher());
		selectButton.setOnAction(e->selectStudent());
		updateSelectButton.setOnAction(e->updateSelectStudent());
	}

	private void addTeacher() {
		teacherForAdd = new Teacher();
		try {
			teacherForAdd.setId((long) Integer.parseInt(addForId.getText()));
			teacherForAdd.setName(addForName.getText());
			teacherDAO.save(teacherForAdd);
			alert.display("添加成功");
		} catch (Exception e) {
			alert.display("添加失败");
		}
	}
	
	private void updateTeacher() {
		try {
			teacherForUpdate = teacherDAO.findById((long) Integer.parseInt(updateForId.getText()));
			if(teacherForUpdate==null) {
				alert.display("不存在该老师");
			}else {
				teacherForUpdate.setName(updateForName.getText());
				teacherDAO.update(teacherForUpdate);
				alert.display("更新成功");
			}
		} catch (Exception e) {
			alert.display("更新失败");
		}
	}
	
	private void deleteTeacher() {
		try {
			teacherForDelete = teacherDAO.findById((long) Integer.parseInt(deleteForId.getText()));
			if(teacherForDelete==null) {
				alert.display("不存在该老师");
			}else {
				teacherForDelete.setName(deleteForName.getText());
				teacherDAO.delete(teacherForDelete);
				alert.display("删除成功");
			}
		} catch (Exception e) {
			alert.display("删除失败");
		}
	}
	
	private void showTeacher() {
		try {
			teacherForShow = teacherDAO.findById((long) Integer.parseInt(showForId.getText()));
			System.out.println(teacherForShow.toString());
			if(teacherForShow==null) {
				alert.display("该老师不存在");
			}else {
				alert.display(teacherForShow.toString());
			}
		} catch (Exception e) {
			alert.display("展示失败");
		}
	}
	
	private void selectStudent() {
		try {
			teacherForSelected = teacherDAO.findById((long) Integer.parseInt(selectedForTutorId.getText()));
			if(teacherForSelected==null) {
				alert.display("该老师不存在");
			}else {
				studentForSelected = studentDAO.findById((long) Integer.parseInt(selectForId.getText()));
				if(studentForSelected==null) {
					alert.display("该学生不存在");
				}else {
					studentForSelected.setTeacher(teacherForSelected);
					studentDAO.update(studentForSelected);
					/*studentSet = teacherForSelected.getStudents();
					studentSet.add(studentForSelected);
					teacherForSelected.setStudents(studentSet);
					teacherDAO.update(teacherForSelected);*/
					alert.display("选择成功");
				}
			}
		} catch (Exception e) {
			alert.display("选择失败");
		}
	}
	
	private void updateSelectStudent() {
		try {
			teacherForSelected = teacherDAO.findById((long) Integer.parseInt(updateSelectForTutorId.getText()));
			if(teacherForSelected==null) {
				alert.display("该老师不存在");
			}else {
				studentForSelected = studentDAO.findById((long) Integer.parseInt(updateSelectForId.getText()));
				if(studentForSelected==null) {
					alert.display("该学生不存在");
				}else {
					/*Teacher lastTeacher = studentForSelected.getTeacher();
					if(lastTeacher!=null) {
						studentSet=lastTeacher.getStudents();
						studentSet.remove(studentForSelected);
						lastTeacher.setStudents(studentSet);
						teacherDAO.update(lastTeacher);
					}*/
					studentForSelected.setTeacher(teacherForSelected);
					studentDAO.update(studentForSelected);
					/*studentSet = teacherForSelected.getStudents();
					studentSet.add(studentForSelected);
					teacherForSelected.setStudents(studentSet);
					teacherDAO.update(teacherForSelected);*/
					alert.display("更新选择成功");
				}
			}
		} catch (Exception e) {
			alert.display("更新选择失败");
		}
	}
}
